package com.zenithmind.user.pojo.domain.valueobject;

import lombok.Value;
import lombok.Builder;
import io.swagger.v3.oas.annotations.media.Schema;

import java.util.Objects;
import java.util.regex.Pattern;

/**
 * 用户档案值对象
 * 遵循单一职责原则：只负责用户基本档案信息
 * 遵循封装原则：不可变对象，确保数据一致性
 */
@Value
@Builder(toBuilder = true)
@Schema(description = "用户档案信息")
public class UserProfile {
    
    private static final Pattern EMAIL_PATTERN = Pattern.compile(
        "^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}$"
    );
    
    private static final Pattern MOBILE_PATTERN = Pattern.compile(
        "^1[3-9]\\d{9}$"
    );

    @Schema(description = "昵称")
    String nickname;

    @Schema(description = "头像URL")
    String avatar;

    @Schema(description = "邮箱")
    String email;

    @Schema(description = "手机号")
    String mobile;

    @Schema(description = "个人简介")
    String intro;

    /**
     * 创建用户档案
     */
    public static UserProfile create(String nickname, String email, String mobile, String intro, String avatar) {
        return UserProfile.builder()
                .nickname(validateNickname(nickname))
                .email(validateEmail(email))
                .mobile(validateMobile(mobile))
                .intro(validateIntro(intro))
                .avatar(avatar)
                .build();
    }

    /**
     * 创建最小用户档案（仅邮箱）
     */
    public static UserProfile minimal(String email) {
        return UserProfile.builder()
                .email(validateEmail(email))
                .build();
    }

    /**
     * 更新昵称
     */
    public UserProfile withNickname(String nickname) {
        return this.toBuilder()
                .nickname(validateNickname(nickname))
                .build();
    }

    /**
     * 更新邮箱
     */
    public UserProfile withEmail(String email) {
        return this.toBuilder()
                .email(validateEmail(email))
                .build();
    }

    /**
     * 更新手机号
     */
    public UserProfile withMobile(String mobile) {
        return this.toBuilder()
                .mobile(validateMobile(mobile))
                .build();
    }

    /**
     * 更新个人简介
     */
    public UserProfile withIntro(String intro) {
        return this.toBuilder()
                .intro(validateIntro(intro))
                .build();
    }

    /**
     * 更新头像
     */
    public UserProfile withAvatar(String avatar) {
        return this.toBuilder()
                .avatar(avatar)
                .build();
    }

    /**
     * 验证昵称
     */
    private static String validateNickname(String nickname) {
        if (nickname != null && nickname.length() > 50) {
            throw new IllegalArgumentException("昵称长度不能超过50个字符");
        }
        return nickname;
    }

    /**
     * 验证邮箱
     */
    private static String validateEmail(String email) {
        if (email == null || email.trim().isEmpty()) {
            throw new IllegalArgumentException("邮箱不能为空");
        }
        if (email.length() > 100) {
            throw new IllegalArgumentException("邮箱长度不能超过100个字符");
        }
        if (!EMAIL_PATTERN.matcher(email).matches()) {
            throw new IllegalArgumentException("邮箱格式不正确");
        }
        return email.toLowerCase().trim();
    }

    /**
     * 验证手机号
     */
    private static String validateMobile(String mobile) {
        if (mobile != null && !mobile.trim().isEmpty()) {
            if (!MOBILE_PATTERN.matcher(mobile).matches()) {
                throw new IllegalArgumentException("手机号格式不正确");
            }
        }
        return mobile;
    }

    /**
     * 验证个人简介
     */
    private static String validateIntro(String intro) {
        if (intro != null && intro.length() > 255) {
            throw new IllegalArgumentException("个人简介长度不能超过255个字符");
        }
        return intro;
    }

    /**
     * 检查档案是否完整
     */
    public boolean isComplete() {
        return nickname != null && !nickname.trim().isEmpty() &&
               email != null && !email.trim().isEmpty();
    }

    /**
     * 获取显示名称（优先使用昵称，否则使用邮箱前缀）
     */
    public String getDisplayName() {
        if (nickname != null && !nickname.trim().isEmpty()) {
            return nickname;
        }
        if (email != null && email.contains("@")) {
            return email.substring(0, email.indexOf("@"));
        }
        return "未知用户";
    }
}
